package com.drore.cloud.sdkjdbc.utils;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/***
 * 浙江卓锐科技股份有限公司 版权所有@Copyright 2016
 * 说明
 * 项目名称
 * @since:cloud-ims 1.0
 * @author <a href="mailto:baoec@drore.com">baoec@drore.com </a> 
 * 2018/04/25 16:35
 */

public class JdbcUtils {
    private DataSource dataSource;
    public static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
    }
    public static void closeStatement(Statement statement){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeConnection(Connection connection){
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static List<Map<String,Object>> readDataLine(ResultSet rs) throws SQLException {
        ArrayList<Map<String,Object>> result = Lists.newArrayList();
        while (rs.next()){
            ResultSetMetaData metaData = rs.getMetaData();
            Map<String,Object> info= Maps.newHashMap();
            int columnCount = metaData.getColumnCount();
            for (int i=1;i<=columnCount;i++){
                String columnLabel = metaData.getColumnLabel(i);
                int columnType = metaData.getColumnType(i);
                Object value=null;
                switch (columnType){
                    case Types.DATE:
                        //Zero date value prohibited
                        //防止报时区错误,改成getString
                        value=rs.getString(columnLabel);
                        break;
                    case Types.TIME:
                        value=rs.getString(columnLabel);
                        break;
                    case Types.TIMESTAMP:
                        value=rs.getString(columnLabel);
                        break;
//                    case Types.TIME_WITH_TIMEZONE:
//                        value=rs.getString(columnName);
//                        break;
//                    case Types.TIMESTAMP_WITH_TIMEZONE:
//                        value=rs.getString(columnName);
//                        break;
                    default:
                        value=rs.getObject(columnLabel);
                        break;
                }
                info.put(columnLabel,value);
            }
            result.add(info);
        }

        return result;

    }


    public static List<Map<String,Object>> query_list(ResultSet rs) throws SQLException {
        ArrayList<Map<String,Object>> result = Lists.newArrayList();
        while (rs.next()){
            ResultSetMetaData metaData = rs.getMetaData();
            Map<String,Object> info= Maps.newHashMap();
            int columnCount = metaData.getColumnCount();
            for (int i=1;i<=columnCount;i++){
                String columnName = metaData.getColumnName(i);
                int columnType = metaData.getColumnType(i);
                Object value=null;
                switch (columnType){
                    case Types.DATE:
                        //Zero date value prohibited
                        //防止报时区错误,改成getString
                        value=rs.getString(columnName);
                        break;
                    case Types.TIME:
                        value=rs.getString(columnName);
                        break;
                    case Types.TIMESTAMP:
                        value=rs.getString(columnName);
                        break;
//                    case Types.TIME_WITH_TIMEZONE:
//                        value=rs.getString(columnName);
//                        break;
//                    case Types.TIMESTAMP_WITH_TIMEZONE:
//                        value=rs.getString(columnName);
//                        break;
                    default:
                        value=rs.getObject(columnName);
                        break;
                }
                info.put(columnName,value);
            }
            result.add(info);
        }

        return result;

    }
}
